﻿using AdverseEventReportSystem.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Utils;

namespace AdverseEventReportSystem.BLL
{
    /// <summary>
    /// 休假管理
    /// </summary>
    public class LeaveManager : BaseManager
    {
        /// <summary>
        /// 保存休假
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        public static OperateResult SaveLeave(Leave leave)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();

                if (leave.Id == 0)
                {
                    SysLoginUser loginUser = LoginManager.GetLoginUser();
                    leave.DepartmentCode = loginUser.CurrDepCode;
                    leave.DepartmentName = loginUser.CurrDepName;
                    leave.CreateUserID = loginUser.Personnel_id;
                    leave.CreateTime = DateTime.Now;
                }
                else
                {
                    ResultHelperOfLeave result = service.GetLeave(leave.Id.ToString());
                    if (result.S)
                    {
                        Leave oldLeave = result.D;
                        oldLeave.UserId = leave.UserId;
                        oldLeave.UserName = leave.UserName;
                        oldLeave.TypeId = leave.TypeId;
                        oldLeave.TypeName = leave.TypeName;
                        oldLeave.StartTime = leave.StartTime;
                        oldLeave.EndTime = leave.EndTime;
                        oldLeave.Days = leave.Days;
                        oldLeave.Reason = leave.Reason;

                        leave = oldLeave;
                    }
                }

                ResultHelperOfBoolean result1 = service.SaveLeave(leave);
                if (!result1.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result1.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        ///  删除休假
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult DelLeave(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfBoolean result = service.DelLeave(id.ToString());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取包含时间段内的所有休假列表
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetLeaveInfoList(string startDate, string endDate, string departmentCode)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfDataTable result = service.GetLeaveInfoList(departmentCode, DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now));

                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return resultInfo;
        }
    }
}
